package com.jeomo.mem.starter.mapper;

import com.jeomo.base.starter.mapper.IBaseMapper;
import com.jeomo.mem.starter.entity.MemberPointsExec;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 会员积分执行信息
 *
 * @author:Jackpot
 * @create 2020-11-10 20:32
 * @description:
 */
@Mapper
public interface MemberPointsExecMapper extends IBaseMapper<MemberPointsExec> {

    /**
     * 查询会员卡的积分执行信息 (已按过期时间顺序排序)
     * @param cardNo    会员卡号
     * @param now       当前时间
     * @return
     */
    @Select("select exe.id,exe.version,exe.card_no,exe.points from member_points_exec as exe " +
            " where exe.card_no=#{cardNo} and exe.points>0 and exe.expired_time>#{now} ORDER BY exe.expired_time ")
    List<MemberPointsExec> queryPointsExe(@Param("cardNo") String cardNo, @Param("now") LocalDateTime now);


    /**
     * 查询会员卡截止到指定时间的剩余积分
     * @param cardNo    会员卡号
     * @param date  截止时间(包含)
     * @return
     */
    @Select("select SUM(exe.points) from member_points_exec as exe where exe.card_no=#{cardNo} and exe.expired_time >= #{date}")
    Integer getPoints(@Param("cardNo") String cardNo, @Param("date") LocalDate date);

    /**
     * 减积分
     * @param id
     * @param version
     * @param points    积分变动
     * @return
     */
    @Update("update member_points_exec set points = points- #{points} where id=#{id} and version=#{version} ")
    int subPoints(@Param("id") Long id,@Param("version") String version,@Param("points") int points);


}
